﻿@using KRM.Core
@using KRM.Core.General
@using KRM.Core.Library
@model KRM.Core.KRMPart
@{
    ViewBag.Title = Model.partID > 0 ? "View part - " + Model.partName + "(" + Model.partID + ")" : "Adding new Part";
    Layout = "~/Views/Shared/_PopupLayout.cshtml";
    tblKRMPart part = ViewBag.Part;
    var barcode = part.PartBarCodeRecords().FirstOrDefault();
    var user = Users.Current(User.Identity.Name);
    var discountedValue = Model.ourTotalIncCarriage * (1 - (Model.tradeDiscount / 100));
    var disprofpound = Math.Round((Model.retailIncVAT == 0) ? 0 : (Model.retailExcVAT - discountedValue), 2);
    var disprofperc = (Model.retailExcVAT == 0) ? 0 : ((disprofpound / Model.retailExcVAT) * 100);
    IEnumerable<vPartsHistory> partsHistory = ViewBag.PartHistory;
    IEnumerable<tblPartChangeLog> changeLogs = ViewBag.ChangeLogs;
    IEnumerable<tblPartImage> partImages = ViewBag.PartImages;
    IEnumerable<vWebStockHistory> stockHistories = ViewBag.StockHistories;
    IEnumerable<LP_Vehicle> verhicles = ViewBag.Vehicles;
    var lnkPart = "";
    var partGroup = part.PartGroup;
    if (partGroup != null && partGroup.GroupID != 0)
    {
        var listGroup = partGroup;
        while (listGroup.GroupName.ToLower() != "root" && listGroup.GroupID != 0)
        {
            lnkPart = " >>" + listGroup.GroupName + lnkPart;
            listGroup = tblKRMPartGroup.ListPartGroup.FirstOrDefault(a => a.GroupID == listGroup.ParentGroup);
        }
    }
    if (string.IsNullOrEmpty(lnkPart))
    {
        lnkPart = ">>Uncategorized";
    }
    var disable = Model.partID > 0 ? "disabled=disabled" : "";
    
}
<link href="~/css/jquery.treeview.css" rel="stylesheet" />
<script src="~/js/jquery.treeview.min.js"></script>
<script>
    $(document).ready(function () {
        $("#tree").treeview({
            animated: "fast",
            collapsed: true,
        });
    });
</script>
<script src="/js/stockcontrol.js"></script>
<div class="padding10">
    <form action="/stockcontrol/editpartname" method="POST" id="fr-editpart" class="margin0">
        <input type="hidden" id="hfpartid" value="@Model.partID" name="partid"/>
        <input type="hidden" id="hfpartgroupid" name="partgroupid" value="@(partGroup != null ? partGroup.GroupID.ToString() : "")" />
        <input type="hidden" id="hfvehicleid" name="vehicleid" />
        <input type="hidden" id="hfversion" value="@Model.version" />
        <div class="row-fluid">
            <input type="submit" class="btn btn-primary" value="Save change" />
            <a href="#parthistory" role="button" data-toggle="modal" class="btn btn-info">Part History</a>
            <a href="#partimages" role="button" data-toggle="modal" class="btn btn-primary">Part Images</a>
        </div>
        <fieldset>
            <legend>Part Details - @Model.partName</legend>
            <div class="row-fluid">
                <div class="span7">Part Group: <a href="#partGroup" role="button" data-toggle="modal" style="padding-left: 5px" class="partgrouplink">@lnkPart</a></div>
                <div class="span2 text-right">Version</div>
                <div class="span3">
                    <input type="text" class="input-block-level" value="@Model.version" name="version" id="tbversion" />
                </div>
            </div>
            <input type="hidden" name="partid" value="@Model.partID"/>
            <div class="row-fluid">
                <div class="span2">Part Name</div>
                <div class="span4">
                    <input type="text" class="input-block-level" value="@Model.partName" name="partname" required="required" />
                </div>
                <div class="span1">
                </div>
                <div class="span2 text-right">Part Code</div>
                <div class="span3">
                    <input type="text" class="input-block-level" value="@Model.partCode" name="partcode" />
                </div>
            </div>
            <div class="row-fluid">
                <div class="span2">Supplier</div>
                <div class="span3">
                    @Html.DropDownList("CompanyID", null, new { @class = "input-block-level" })
                </div>
                <div class="span4 text-right">Unit Forecast</div>
                <div class="span3">
                    <input type="text" class="input-block-level" value="@Model.forecast" name="forecastUnits" />
                </div>
            </div>
            <div class="row-fluid">
                <div class="span2">Factory</div>
                <div class="span3">
                    @Html.DropDownList("FactoryID", null, new { @class = "input-block-level", disable })
                </div>
                <div class="span4 text-right">Supplier Leadtime</div>
                <div class="span2">
                    <input type="text" class="input-block-level" value="@Model.supplierLeadTime" name="SupplierLeadtime" />
                </div>
                <div class="span1">Days</div>
            </div>
            <div class="row-fluid">
                <div class="span7">
                    <label>
                        <input class="cbcheck" type="checkbox" name="discontinued" @(Model.Discontinued ? "checked=checked" : "") />
                        Discontinued
                    </label>
                </div>
                <div class="span2 text-right">Production Points</div>
                <div class="span2">
                    <input type="text" class="input-block-level" value="@part.ProductionPoints" name="ProductionPoints" />
                </div>
            </div>
            <div class="row-fluid">
                <div class="span1">Barcode</div>
                <div class="span3">
                    <input type="text" class="input-block-level" name="barcode" value="@(barcode != null ? barcode.BarCode : "")" />
                </div>
                <div class="span1 text-right">Warehouse</div>
                <div class="span3">
                    @Html.DropDownList("ManufacturingWarehouseID", null, "Please select...", new { @class = "input-block-level" })
                </div>
                <div class="span1 text-right">Carrier</div>
                <div class="span3">
                    @Html.DropDownList("CarrierServiceID", null, new { @class = "input-block-level", onchange = "carrierservicechange($(this)," + Model.partID + ");" })
                </div>
            </div>
            <div class="row-fluid">
                <div class="span3">Product Classification Code:</div>
                <div class="span3">
                    <input type="text" class="input-block-level" value="@part.ProductClassificationCode" name="ProductClassificationCode" />
                </div>
                <div class="span2 text-right">FSC Certification:</div>
                <div class="span4">
                    @Html.DropDownList("CertificationID", null, new { @class = "input-block-level" })
                </div>
            </div>
            <div class="row-fluid">
                <div class="span1">Weight</div>
                <div class="span2">
                    <input type="text" style="width: 60%" value="@Model.weight" name="weightKG" />
                    kg
                </div>
                <div class="span1">Packages</div>
                <div class="span2">
                    <input type="text" class="input-block-level" value="@Model.packages" name="packages" />
                </div>
                <div class="span1">
                </div>
                <div class="span2 text-right">Parcel Type</div>
                <div class="span3">
                    @Html.DropDownList("PackageTypeID", null, new { @class = "input-block-level" })
                </div>
            </div>
            <div class="row-fluid">
                <div class="span1">Unloading Time</div>
                <div class="span3">
                    <input type="text" style="width: 40%" value="@part.DeliveryUnloadingTime" name="DeliveryUnloadingTime" />
                    <span style="font-style: italic">Seconds</span>
                </div>
                <div class="span1">Assembly Time</div>
                <div class="span3">
                    <input type="text" style="width: 40%" value="@part.TimeForAssembly" name="TimeForAssembly" />
                    <span style="font-style: italic">Seconds</span>
                </div>
                <div class="span2 text-right">Delivery Volume</div>
                <div class="span2">
                    <input type="text" class="input-block-level" value="@part.DeliveryVolume" name="DeliveryVolume" />
                </div>
            </div>
            <div class="row-fluid">
                <div class="span1">Vehicle:</div>
                <div class="span3"><a href="#SelectVehicles" role="button" data-toggle="modal" style="padding-left: 5px" class="VehiclesLink">Not specific</a></div>
                <div class="span2">20' Container Qty</div>
                <div class="span2">
                    <input type="text" class="input-block-level" value="@part.Container20Qty" name="Container20Qty" />
                </div>
                <div class="span2 text-right">40' Container Qty</div>
                <div class="span2">
                    <input type="text" class="input-block-level" value="@part.Container40Qty" name="Container40Qty" />
                </div>
            </div>
        </fieldset>
        <fieldset>
            <legend>Stock Details</legend>
            <div class="row-fluid">
                <div class="span2">Picking Note:</div>
                <div class="span4">
                    <textarea class="input-block-level" rows="3" name="PickingNotes">@part.PickingNotes</textarea>
                </div>
                <div class="span6">
                    <div class="row-fluid" style="margin-bottom: 10px">
                        <div class="span6">
                            <label>
                                <input type="checkbox" class="cbcheck" @(!Model.stocked ? "readonly=readonly" : "") @(Model.containerStock ? "checked=checked" : "") name="containerpart" />
                                Container part</label>
                        </div>
                        <div class="span6">
                            <a href="#stochistory" role="button" data-toggle="modal" class="btn btn-primary">Stock History</a>
                        </div>
                    </div>
                    <div class="row-fluid">
                        <div class="span6">
                            <label>
                                <input type="checkbox" class="cbcheck" @(Model.stocked ? "checked=checked" : "") disabled="disabled" />
                                Stock Controlled</label>
                        </div>
                        <div class="span6">
                        </div>
                    </div>
                </div>
            </div>
        </fieldset>
        @if (user.HasPermission(Permissions.CanViewTradePriceInformation) && Model.partID > 0)
        {
            <div class="border-padding10">
                <div class="row-fluid">
                    <div class="span2">Normal Trade: <strong>£</strong></div>
                    <div class="span3">
                        <input type="text" class="input-block-level" name="NormalTrade" value="@Model.normalTrade.ToString("f2")" />
                    </div>
                    <div class="span1"></div>
                    <div class="span2">Retail ex VAT: <strong>£</strong></div>
                    <div class="span3">
                        <input type="text" class="input-block-level" id="RetailExcVAT" name="retailExcVAT" value="@General.strPounds(Math.Round(Model.retailExcVAT, 2))" readonly="readonly" />
                    </div>
                </div>
                <div class="row-fluid">
                    <div class="span2">Our Trade: <strong>£</strong></div>
                    <div class="span3">
                        <input type="text" class="input-block-level" id="outtrade" name="outtrade" value="@Model.tradeCost.ToString("f2")" />
                    </div>
                    <div class="span1"></div>
                    @if (Model.IncVAT == null)
                    {
                        <div class="span2">
                            <label>
                                <input type="checkbox" class="cbcheck" name="cbvat" onchange="vatchange('false','false')" id="cbvat" @(Model.IncVAT != null ? "checked=checked" : "") />
                                VAT: <strong>%</strong></label>
                        </div>
                        <div class="span3">
                            <input type="text" class="input-block-level" id="incVAT" value="@(Model.IncVAT != null ? Model.IncVAT.ToString() : tblCompanyVariable.VAT.ToString())" name="IncVAT" readonly="readonly" />
                        </div>
                    }

                </div>
                <div class="row-fluid">
                    <div class="span2">Carriage: <strong>£</strong></div>
                    <div class="span3">
                        <input type="text" class="input-block-level" id="Carriage" readonly="readonly" value="@Model.singleCalculatedCarriage.ToString("f2")" />
                    </div>
                    <div class="span1"></div>
                    <div class="span2">Retail inc VAT: <strong>£</strong></div>
                    <div class="span3">
                        <input type="text" class="input-block-level" id="RetailIncVAT" value="@Model.retailIncVAT.ToString("f2")" />
                    </div>
                </div>
                <div class="row-fluid">
                    <div class="span5">
                        <div class="row-fluid">
                            <div class="span5">Total Inc Carriage: <strong>£</strong></div>
                            <div class="span7">
                                <input type="text" class="input-block-level" id="OurTradeIncCarriage" readonly="readonly" value="@General.strPounds(Model.ourTotalIncCarriage)" />
                            </div>
                        </div>
                        <div class="row-fluid">
                            <div class="span5">With Supp Discount: <strong>£</strong></div>
                            <div class="span7">
                                <input type="text" class="input-block-level" id="TradeWithDiscount" readonly="readonly" value="@((Model.ourTotalIncCarriage * (1 - (Model.tradeDiscount / 100))).ToString("f2"))" />
                            </div>
                        </div>
                    </div>
                    <div class="span1"></div>
                    <div class="span6">
                        @if (user.HasPermission(Permissions.CanViewMarginAndRetail))
                        {
                            <fieldset style="margin-top: 0; font-weight: bold">
                                <legend>Margin</legend>
                                <div class="row-fluid" @(Model.profitPercent < 20 ? "style=color:red" : "")>
                                    <div class="span6 ProfitPounds">£@General.strPounds(Model.profitPound)</div>
                                    <div class="span6 ProfitPercent">
                                        @(Model.profitPercent > 0 ? General.strPounds(Model.profitPercent) + "%" : "(" + General.strPounds(Model.profitPercent) + "% )")
                                    </div>
                                </div>
                                <div class="row-fluid" @(disprofperc < 20 ? "style=color:red" : "")>
                                    <div class="span6 ProfPoundsWithDis">
                                        £@General.strPounds(disprofpound)
                                    </div>
                                    <div class="span6 ProfPercWithDis">
                                        @(disprofperc > 0 ? General.strPounds(Math.Round(disprofperc, 2)) + "%" : "(" + General.strPounds(Math.Round(disprofperc, 2)) + "% )")
                                    </div>
                                </div>
                            </fieldset>
                        }
                    </div>
                </div>
            </div>
        }

        <fieldset>
            <legend>Carriage Calculations</legend>
            <fieldset>
                <legend>When bought singularly</legend>
                <div class="row-fluid">
                    <div class="span6">
                        <label>
                            <input type="radio" class="cbcheck" name="radio" value="singularStandSup" id="singularStandSup" onchange="vatchange('true','true')" @(Model.singularStandSup ? "checked=checked" : "") />
                            Standard Supplier carriage charge
                        </label>
                    </div>
                    <div class="span5">
                        <select class="input-block-level" id="cbsingular" name="SupplierDelCharge">
                            <option>Select</option>
                        </select>
                    </div>
                </div>
                <div class="row-fluid">
                    <div class="span6">
                        <label>
                            <input type="radio" class="cbcheck" name="radio" value="singularIndivCharge" id="singularIndivCharge" onchange="vatchange('true','false')" @(Model.partID > 0 ? Model.singularIndivCharge ? "checked=checked" : "" : "checked=checked") />
                            Individual part delivery charge
                        </label>
                    </div>
                    <div class="span3">
                        <input type="text" class="input-block-level" id="singleIndivCharge" name="singularIndivCharge" value="@Model.singularIndivDelcharge.ToString("f2")" />
                    </div>
                </div>
                <div class="row-fluid">
                    <div class="span4">
                    </div>
                    <div class="span2 text-right">Delivery Drag:</div>
                    <div class="span3">
                        <input type="text" class="input-block-level" value="@Model.AverageCharge" name="AverageCharge" />
                    </div>
                </div>
            </fieldset>
        </fieldset>
    </form>
</div>

<div id="parthistory" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="parthistory" aria-hidden="true" data-width="800">
    <div class="modal-body">
        <div class="fieldset">
            <div class="legend"><span>Part History</span></div>
            <div class="overflow-auto max-height200">
                <table class="table table-fix table-bordered">
                    <thead>
                        <tr>
                            <th>DateChange</th>
                            <th>User's Name</th>
                            <th>Part Name</th>
                            <th>Company</th>
                            <th>Trade Price</th>
                            <th>Carriage Charge</th>
                            <th>Supplier RRP</th>
                            <th>Our Retail Price</th>
                            <th>Delivery Time</th>
                            <th>Part Deleted?</th>
                            <th>Kybo Code</th>
                            <th>Supplier Code</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in partsHistory)
                        {
                            <tr>
                                <td>@item.Date_Changed</td>
                                <td>@item.User_s_Name</td>
                                <td>@item.Part_Name</td>
                                <td>@item.Company</td>
                                <td>@item.Trade_Price</td>
                                <td>@item.Carriage_Charge</td>
                                <td>@item.Supplier_RRP</td>
                                <td>@item.Our_Retail_Price</td>
                                <td>@item.Delivery_Time</td>
                                <td>@item.Part_Deleted_</td>
                                <td>@item.Kybo_Code</td>
                                <td>@item.Supplier_Code</td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
        <div class="fieldset">
            <div class="legend"><span>Change Log</span></div>
            <div class="overflow-auto">
                <table class="table table-fix table-bordered">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>PartID</th>
                            <th>UserName</th>
                            <th>Action</th>
                            <th>Comments</th>
                            <th>DateOfChange</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in changeLogs)
                        {
                            <tr>
                                <td>@item.ID</td>
                                <td>@item.PartID</td>
                                <td>@item.UserName</td>
                                <td>@item.Action</td>
                                <td>@item.Comments</td>
                                <td>@item.DateOfChange</td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    </div>
</div>

<div id="partimages" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="partimages" aria-hidden="true" data-width="600">
    <div class="modal-body">
        <table class="table table-fix table-bordered">
            <thead>
                <tr>
                    <th>Image</th>
                    <th>PartImageID</th>
                    <th>PartID</th>
                    <th>ImageLocation</th>
                    <th>Name</th>
                    <th>Position</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in partImages)
                {
                    <tr>
                        <td>
                            <img src="/rdm/getfile?path=@item.ImageLocation" style="width: 80px; height: 60px" alt="@item.Name" /></td>
                        <td>@item.PartImageID</td>
                        <td>@item.PartID</td>
                        <td>@item.ImageLocation</td>
                        <td>@item.Name</td>
                        <td>@item.Position</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    </div>
</div>

<div id="stochistory" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="stochistory" aria-hidden="true" data-width="700">
    <div class="modal-body">
        <table class="table table-fix table-bordered">
            <thead>
                <tr>
                    <th>Date Of Change</th>
                    <th>KRM Login</th>
                    <th>Win Login</th>
                    <th>WareHouseName</th>
                    <th>StartStock</th>
                    <th>EndStock</th>
                    <th>StartPriority</th>
                    <th>EndPriority</th>
                    <th>Reason</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in stockHistories)
                {
                    <tr>
                        <td>@item.Date_of_Change</td>
                        <td>@item.KRM_Login</td>
                        <td>@item.Win_Login</td>
                        <td>@item.WarehouseName</td>
                        <td>@item.StartStock</td>
                        <td>@item.EndStock</td>
                        <td>@item.StartPriority</td>
                        <td>@item.EndPriority</td>
                        <td>@item.Reason</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    </div>
</div>

<div id="partGroup" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="partGroup" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
        <h5>Select Part Group</h5>
    </div>
    <div class="modal-body">
        <div class="group-filter-tree-bg">
            @Html.Raw(ViewBag.Tree)
        </div>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary w90" onclick="selectdpartgroupid()">Ok</button>
        <button class="btn w90" data-dismiss="modal" aria-hidden="true">Cancel</button>
    </div>
</div>

<div id="partVersionChangeLog" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="partVersionChangeLog" aria-hidden="true">
    <form action="/stockcontrol/partVersionChangeLog" class="margin0" id="fr-partVersionChangeLog" method="POST">
        <input type="hidden" name="partid" value="@Model.partID"/>
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
            <h5>Part Version Change Log</h5>
        </div>
        <div class="modal-body">
            <div class="row-fluid">
                <div class="span4">Action</div>
                <div class="span8">
                    @Html.DropDownList("ActionID", null, new { @class = "input-block-level" })
                </div>
            </div>
            <div class="row-fluid">
                <div class="span4">Comments</div>
                <div class="span8">
                    <textarea name="comments" rows="5" class="input-block-level" required="required"></textarea>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" type="submit">Save and Close</button>
            <button class="btn w90" data-dismiss="modal" aria-hidden="true">Cancel</button>
        </div>
    </form>
</div>

<div id="SelectVehicles" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="SelectVehicles" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
        <h5>Select Vehicles</h5>
    </div>
    <div class="modal-body">
        <div class="row-fluid">
            <div class="span5">
                <div class="border-padding10 lstvehicles" style="height: 258px">
                    @foreach (var item in verhicles)
                    {
                        <label>
                            <input type="checkbox" class="cbcheck vehicle-item"  value="@item.VehicleID" />
                            @item.Name</label>
                    }
                </div>
            </div>
            <div class="span7 vehicledetail">
                <div class="row-fluid">
                    <div class="span4">Vehicle type</div>
                    <div class="span8">
                        <input type="text" class="input-block-level" /></div>
                </div>
                <div class="row-fluid">
                    <div class="span4">Vehicle code</div>
                    <div class="span8">
                        <input type="text" class="input-block-level" /></div>
                </div>
                <div class="row-fluid">
                    <div class="span4">Max weight</div>
                    <div class="span8">
                        <input type="text" class="input-block-level" /></div>
                </div>
                <div class="row-fluid">
                    <div class="span4">Max volume</div>
                    <div class="span8">
                        <input type="text" class="input-block-level" /></div>
                </div>
                <div class="row-fluid">
                    Description:
                    <textarea class="input-block-level" rows="5"> </textarea>
                </div>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary w90" onclick="selectdvehicleid()">Ok</button>
        <button class="btn w90" data-dismiss="modal" aria-hidden="true">Cancel</button>
    </div>
</div>

@if (Model.partID > 0)
{
    <script>
        $(document).ready(function () {
            vatchange('false', 'false');
        });
    </script>
}
